package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.model.entity.Testpackage;
import com.qf.service.TestpackageService;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (Testpackage)表控制层
 *
 * @author makejava
 * @since 2024-05-01 12:16:25
 */
@RestController
@RequestMapping("/api/testpackage")
public class TestpackageController {
    /**
     * 服务对象
     */
    @Autowired
    private TestpackageService testpackageService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list/page")
    public R queryByPage(@RequestParam Map<String,Object> map) {

        System.out.println("map = " + map);

        int pageNum = Integer.valueOf((String) map.get("pageNum"));
        int pageSize = Integer.valueOf((String) map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        List<Testpackage> list =  testpackageService.testPackageByCondition(map);

        if (list!=null && list.size()>0){

            PageInfo<Testpackage> pageInfo = new PageInfo<>(list);

            return R.ok(pageInfo);


        }

        return R.fail("查询失败");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return R.ok(this.testpackageService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param testpackage 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(Testpackage testpackage, HttpSession session) {

      boolean result =   this.testpackageService.insert(testpackage,session);

      if (result){
          return R.ok();
      }

        return R.fail("增加失败");
    }

    /**
     * 编辑数据
     *
     * @param testpackage 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(Testpackage testpackage) {

        boolean result = this.testpackageService.update(testpackage);

        if (result){
            return R.ok();
        }
        return R.fail("添加失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Integer id) {

     boolean  result =    this.testpackageService.deleteById(id);

     if (result){
         return R.ok();
     }

        return R.fail("删除失败");
    }


    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer> list){

        boolean result = testpackageService.deleteBatch(list);

        if (result){
            return R.ok();
        }
        return R.fail("删除失败");
    }


    @RequestMapping("/testpackageExport")
    public void testpackageExport(HttpServletResponse response){

        testpackageService.testpackageExport(response);

    }


    @RequestMapping("importTestPackage")
    public R importTestPackage(@RequestPart MultipartFile file){


       return testpackageService.importTestPackageExcel(file);

    }





}

